use crate::solution::Solution;

impl Solution {
    pub fn max_length_between_equal_characters(s: String) -> i32 {
        let mut idx = vec![-1; 26];
        let mut ans = -1;
        for (i, b) in s.bytes().enumerate() {
            if idx[(b - b'a') as usize] != -1 {
                ans = ans.max(i as i32 - idx[(b - b'a') as usize] - 1);
            } else {
                idx[(b - b'a') as usize] = i as i32;
            }
        }
        ans
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_1() {
        let s = "aa";
        let res = Solution::max_length_between_equal_characters(s.parse().unwrap());
        println!("{}", res);
        assert_eq!(res, 0)
    }

    #[test]
    fn test_2() {
        let s = "scayofdzca";
        let res = Solution::max_length_between_equal_characters(s.parse().unwrap());
        println!("{}", res);
        assert_eq!(res, 6)
    }

}
